//
//  WeiboModel.m
//  weibo-01
//
//  Created by HZApple on 16/5/11.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "WeiboModel.h"
#import "RegexKitLite.h"

@implementation WeiboModel

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {

    NSString *regex = @"\\[\\w+\\]";
    NSArray *faceArray = [self.text componentsMatchedByRegex:regex];
    
    NSArray *facePlistArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"]];
    
    for (NSString *faceString in faceArray) {
        NSString *condition = [NSString stringWithFormat:@"chs = '%@'",faceString];
        NSArray * result = [facePlistArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:condition]];
        NSDictionary *faceDic = [result firstObject];
        
        NSString *imageName = faceDic[@"png"];
        
        NSString *imageURL = [NSString stringWithFormat:@"<image url = '%@'>",imageName];
        
        self.text = [self.text stringByReplacingOccurrencesOfString:faceString withString:imageURL];
    }
    
    return YES;
}


@end
